package com.mutual.product.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mutual.attachment.pojo.Attachment;
import com.mutual.attachment.service.AttachmentService;
import com.mutual.commom.pojo.ReturnResult;
import com.mutual.commom.util.SystemUtil;
import com.mutual.common.BaseController;
import com.mutual.common.ResultList;
import com.mutual.product.pojo.Product;
import com.mutual.product.service.ProductService;

@Controller
@RequestMapping("/product")
public class ProductController extends BaseController{

	@Resource
	private ProductService productService;
	
	@Resource
	private AttachmentService attachmentService;
	
	
	@RequestMapping(value = "/toList")
	public String list(){
		return "product/productList";
	}
	
	@RequestMapping(value = "/listData")
	@ResponseBody
	public ResultList<Product> listData(@RequestParam Map<String,Object> param){
		total=productService.selectProductTotal(param);
		ResultList<Product> result=new ResultList<>();
		result.setCount(total);
		List<Product> productList=productService.selectProductList(param);
		result.setData(productList);
		return result;
	}
	
	@RequestMapping(value = "/toAddProduct")
	public String toAddProduct(){
		return "product/addProduct";
	}
	
	
	@RequestMapping(value = "/addProduct")
	@ResponseBody
	public ReturnResult addProduct(@RequestParam Map<String,Object> param,Product product){
		ReturnResult returnResult=null;
		try {
			Attachment attachment=new Attachment();
			attachment.setId(SystemUtil.getInt(param.get("attachmentId")));
			attachment.setType(Attachment.TYPE_PRODUCT);
			returnResult = productService.addProduct(product, attachment);
		} catch (Exception e) {
			returnResult=new ReturnResult();
			returnResult.setReturnMsg("添加异常");
			returnResult.setStatus(ReturnResult.STATUS_FAILED);
		}
		return returnResult;
	}
	
	@RequestMapping(value = "/toUpdateProduct")
	public String toUpdateProduct(int id,Model model){
		Product product=productService.selectByPrimaryKey(id);
		Attachment attachment=new Attachment();
		attachment.setType(Attachment.TYPE_PRODUCT);
		attachment.setMoudleId(id);
		List<Attachment> attachments=attachmentService.selectAttachmentByMoudleId(attachment);
		if(!attachments.isEmpty()){
			model.addAttribute("attachment", attachments.get(0));
		}
		model.addAttribute("product", product);
		return "product/updateProduct";
	}
	
	@RequestMapping(value = "/updateProduct")
	@ResponseBody
	public ReturnResult updateProduct(@RequestParam Map<String,Object> param,Product product){
		ReturnResult returnResult=null;
		try {
			Attachment attachment=new Attachment();
			attachment.setId(SystemUtil.getInt(param.get("attachmentId")));
			attachment.setType(Attachment.TYPE_PRODUCT);
			returnResult = productService.updateProduct(product, attachment);
		} catch (Exception e) {
			returnResult=new ReturnResult();
			returnResult.setReturnMsg("添加异常");
			returnResult.setStatus(ReturnResult.STATUS_FAILED);
		}
		return returnResult;
	}
	
	@RequestMapping(value = "/deleteProduct")
	@ResponseBody
	public boolean deleteProduct(int id){
		boolean flag=false;
		try {
			productService.deleteProduct(id);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
}
